﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace CeA.Reports.Atendimento.Web.WebControls {
    public enum MessageBoxCommand { 
        OK,
        Yes,
        No,
        Cancel
    }
    
    public enum MessageBoxButtoms {
        OK,
        CancelOK,
        YesNo,
        YesNoCancel,
        YesCancel,
    }
    
    public partial class MessageBox : System.Web.UI.UserControl {
        
        #region -- Event Model ----------------------------------------------------

        // Event Keys
        private static readonly object CommandEventKey = new object();


        // Delegates
        public delegate void CommandEventHandler(object sender, MessageBoxEventArgs e);


        // Events
        public event CommandEventHandler Command {
            add {
                Events.AddHandler(CommandEventKey, value);
            }
            remove {
                Events.RemoveHandler(CommandEventKey, value);
            }
        }


        protected virtual void OnCommand(MessageBoxEventArgs e) {
            CommandEventHandler handler = Events[CommandEventKey] as CommandEventHandler;
            if (handler != null) {
                handler(this, e);
            }
        }
        #endregion


        public string CommandName                                                   {
            get {
                return Convert.ToString(ViewState["m_commandName"]);
            }
            set {
                ViewState.Add("m_commandName", value);
            }
        }
        /*
        public string Titulo {
            get {
                return lblTitle.Text;
            }
            set {
                lblTitle.Text = value;
            }
        }


        public string Mensagem {
            get {
                return lblMensagem.Text;
            }
            set {
                lblMensagem.Text = value;
            }
        }
        */

        protected void Page_Init()                                                  {
            cmdOK.Click += new EventHandler(cmdOK_Click);
            cmdCancel.Click += new EventHandler(cmdCancel_Click);
            cmdYes.Click += new EventHandler(cmdYes_Click);
            cmdNo.Click += new EventHandler(cmdNo_Click);
        }


        protected void Page_Load(object sender, EventArgs e)                        {
            if (!Page.IsPostBack) {
                string script = "document.getElementById('" + popup.ClientID + "').style.display = 'none';";

                    cmdOK.Attributes.Add("OnClick", script);
                cmdCancel.Attributes.Add("OnClick", script);
                   cmdYes.Attributes.Add("OnClick", script);
                    cmdNo.Attributes.Add("OnClick", script);
            }
        }


        protected void cmdOK_Click(object sender, EventArgs e)                      {
            OnCommand(new MessageBoxEventArgs(MessageBoxCommand.OK, CommandName));
            //popup.Visible = false;
            //Page.ClientScript.RegisterStartupScript(this.GetType(), "closePop",
            //    "document.getElementById('form').style.display='none';", 
            //    true);
        }


        protected void cmdCancel_Click(object sender, EventArgs e)                  {
            OnCommand(new MessageBoxEventArgs(MessageBoxCommand.Cancel, CommandName));
            //popup.Visible = false;
            //Page.ClientScript.RegisterStartupScript(this.GetType(), "closePop",
            //    "document.getElementById('form').style.display='none';",
            //    true);

        }


        protected void cmdYes_Click(object sender, EventArgs e)                     {
            OnCommand(new MessageBoxEventArgs(MessageBoxCommand.Yes, CommandName));
            //popup.Visible = false;
            //Page.ClientScript.RegisterStartupScript(this.GetType(), "closePop",
            //    "document.getElementById('form').style.display='none';",
            //    true);
        }


        protected void cmdNo_Click(object sender, EventArgs e)                      {
            OnCommand(new MessageBoxEventArgs(MessageBoxCommand.No, CommandName));
            //popup.Visible = false;
            
            //Page.ClientScript.RegisterStartupScript(this.GetType(), "closePop",
            //    "document.getElementById('form').style.display='none';",
            //    true);
        }


        public void pop(string title, string message, MessageBoxButtoms buttoms)    {
            enableButtoms(buttoms);
            //popup.Visible = true;

            Page.ClientScript.RegisterStartupScript(this.GetType(), "OpenPop",
                "document.getElementById('form').style.display='block';",
                true);

            lblTitle.Text = title;
            lblMensagem.Text = message;
        }


        public void pop(string title, string message)                               {
            enableButtoms(MessageBoxButtoms.OK);
            //popup.Visible = true;

            Page.ClientScript.RegisterStartupScript(this.GetType(), "OpenPop",
                "document.getElementById('form').style.display='block';",
                true);

            lblTitle.Text = title;
            lblMensagem.Text = message;
        }

        
        public void pop(string message, MessageBoxButtoms buttoms)                  {
            enableButtoms(buttoms);
            //popup.Visible = true;

            Page.ClientScript.RegisterStartupScript(this.GetType(), "OpenPop",
                "document.getElementById('form').style.display='block';",
                true);
            
            lblTitle.Text = "Mensagem";
            lblMensagem.Text = message;
        }


        public void pop(string message)                                             {
            enableButtoms(MessageBoxButtoms.OK);
            //popup.Visible = true;

            Page.ClientScript.RegisterStartupScript(this.GetType(), "OpenPop",
                "document.getElementById('form').style.display='block';",
                true);

            lblTitle.Text = "Mensagem";
            lblMensagem.Text = message;
        }


        private void enableButtoms(MessageBoxButtoms buttoms)                       {
            cmdOK.Visible = false;
            cmdCancel.Visible = false;
            cmdYes.Visible = false;
            cmdNo.Visible = false;
            
            switch (buttoms) {
                case MessageBoxButtoms.OK:
                    cmdOK.Visible = true;
                    break;

                case MessageBoxButtoms.CancelOK:
                    cmdOK.Visible = true;
                    cmdCancel.Visible = true;
                    break;

                case MessageBoxButtoms.YesNo:
                    cmdYes.Visible = true;
                    cmdNo.Visible = true;
                    break;

                case MessageBoxButtoms.YesNoCancel:
                    cmdYes.Visible = true;
                    cmdNo.Visible = true;
                    cmdCancel.Visible = true;
                    break;

                case MessageBoxButtoms.YesCancel:
                    cmdYes.Visible = true;
                    cmdCancel.Visible = true;
                    break;
            }
        }
    }
}